package com.leetcode.www;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

/**
 * Creared with IntelliJ IDEA.
 * Description:
 * User:yxd
 * Date:2022-05-01
 * Time:21:09
 */
public class Everyday1035 {
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        List<Integer> nums1 = new ArrayList<Integer>();
        List<Integer> nums2 = new ArrayList<Integer>();
        inorder(root1, nums1);
        inorder(root2, nums2);

        List<Integer> merged = new ArrayList<Integer>();
        int p1 = 0, p2 = 0;
        while (true) {
            if (p1 == nums1.size()) {
                merged.addAll(nums2.subList(p2, nums2.size()));
                break;
            }
            if (p2 == nums2.size()) {
                merged.addAll(nums1.subList(p1, nums1.size()));
                break;
            }
            if (nums1.get(p1) < nums2.get(p2)) {
                merged.add(nums1.get(p1++));
            } else {
                merged.add(nums2.get(p2++));
            }
        }
        return merged;
    }
    public  void inorder(TreeNode node, List<Integer> res) {
        if (node != null) {
            inorder(node.left, res);
            res.add(node.val);
            inorder(node.right, res);
        }
    }


    public List<Integer> getAllElements1(TreeNode root1, TreeNode root2) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        f(root1,queue);
        f(root2,queue);
        List<Integer> list = new ArrayList<>();
        while(!queue.isEmpty()){
            list.add(queue.poll());
        }
        return list;
    }
    public void f(TreeNode root,PriorityQueue<Integer> queue){
        if(root == null){
            return;
        }
        f(root.left,queue);
        queue.offer(root.val);

        f(root.right,queue);
    }

}
